package com.yc.shopping.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.yc.shopping.util.DBHelper;


public class GoodDao {
	
	/*
	 * 添加商品信息
	 * 
	 */
	public int add(String gname,String gcode,int tid,String gmark,String gimage,String unit,String weight,int balance,String gdate,float price,float saleprice) throws SQLException {
		String sql = "insert into tb_goods values(seq_100_gid.nextval,?,?,?,?,?,?,?,?,?,?,?,1)";
		return DBHelper.update(sql, gname,gcode,tid,gmark,gimage,unit,weight,balance,gdate,price,saleprice);
	}

	
	/*
	 * 查看所有
	 */
	public List<Map<String,Object>> findAll() throws SQLException{
	String sql = "select gid,gname,gcode,g.tid,gmark,gimage,unit,weight,balance,gdate,price,saleprice,gstate,tname from tb_goods g,tb_goodtype gt where g.tid = gt.tid";
	return DBHelper.selectListMap(sql);
	}
	
	/*
	 * 根据类型名称查看
	 */
	public List<Map<String,Object>> findByTname(String tname) throws SQLException{
		String sql = "select gid,gname,gcode,g.tid,gmark,gimage,unit,weight,balance,gdate,price,saleprice,gstate,tname "
		+ "from tb_goods g,tb_goodtype gt where g.tid = gt.tid and tname = ? ";
		return DBHelper.selectListMap(sql, tname);
		
	}
	
	//根据条形码或商品编号查询
	
	public Map<String,Object> findByidOrcode(String code) throws SQLException{
		String sql = "select gid,gname,gcode,g.tid,gmark,gimage,unit,weight,balance,gdate,price,saleprice,gstate,tname "
				+ "from tb_goods g,tb_goodtype gt where g.tid = gt.tid and gid = ? ";
		String sql2 = "select gid,gname,gcode,g.tid,gmark,gimage,unit,weight,balance,gdate,price,saleprice,gstate,tname "
				+ "from tb_goods g,tb_goodtype gt where g.tid = gt.tid and gcode= ? ";
	Map<String,Object> map = DBHelper.selectOneMap(sql, code);//先code作为编号查询
	if(map == null) {
		return DBHelper.selectOneMap(sql2, code);//条形码查询
	}
	return map;
	
	}
	
	/*
	 * 修改库存
	 */
	public int updateBalance(int gid,int num) throws SQLException {
		String sql = "update tb_goods set balance = balance + ? where gid = ?";
		return DBHelper.update(sql, num,gid);
	}
	
}
